//
//  ProvinceDetailController.m
//  PASystem
//
//  Created by 刘沿 on 16/8/8.
//  Copyright © 2016年 czd. All rights reserved.
//

#import "ProvinceDetailController.h"
#import "DetailView.h"
#import "proviceModel.h"
#import "FirstView.h"
#import "ProviceDetailModel.h"
#import "SecondView.h"


@interface ProvinceDetailController ()

@property (nonatomic,assign) NSInteger currentPage;

@property (nonatomic,strong) NSMutableArray *dataArray;

@property (nonatomic,strong) UITableView *tableView1;
@property (nonatomic,strong) UITableView *tableView2;

@property (nonatomic,strong) UIView *view3;

@property (nonatomic,strong) UIView *firstView;

@end

@implementation ProvinceDetailController

-(NSMutableArray *)dataArray{

    if (_dataArray == nil) {
        
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self loadData];
    
    
}

- (void)loadData{

    self.currentPage = 1;
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
    NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
    NSString *json = [NSString stringWithFormat:@"{'action': detailsHygl,'service': MemberService, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,id:%@}'}",userId,pwdId,userHumber,self.contId];
    
    NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
     
        NSDictionary *dict = JSON[@"baseinfo"];
        
        NSDictionary *attrDict = dict[@"attrs"];
        
        proviceModel *model = [proviceModel mj_objectWithKeyValues:attrDict];
        
        ProviceDetailModel *detailModel = [ProviceDetailModel mj_objectWithKeyValues:attrDict];
        
        
        NSString *str = detailModel.SHAPE;
        NSArray *arr = [str componentsSeparatedByString:@","];
        NSMutableArray *resultStr = [NSMutableArray array];
        for (int i = 0; i <arr.count; i ++) {
            
            NSString *arrstr = arr[i];
            
            if ([arrstr isEqualToString:@"1"]) {
                
              [resultStr addObject:@"贸易型"];
                
            }else if([arrstr isEqualToString:@"2"]){
                
              [resultStr addObject:@"生产型"];
                
            }else if ([arrstr isEqualToString:@"3"]){
                [resultStr addObject:@"服务型"];
                
            }else {
                [resultStr addObject:@"其他"];
                
            }
   
        }
        
        NSString *res = [resultStr componentsJoinedByString:@","];
        
        detailModel.SHAPE = res;

        
        NSArray *arrNature = [str componentsSeparatedByString:@","];
        NSMutableArray *nature = [NSMutableArray array];

        for (int i = 0; i < arrNature.count; i ++) {
            NSString *str = arrNature[i];
            
            if ([str isEqualToString:@"1"]) {
                [nature addObject:@"国有"];
                
            }else if ([str isEqualToString:@"2"]){
                [nature addObject:@"集体"];

            }else if ([str isEqualToString:@"3"]){
                [nature addObject:@"民营"];

            }else if ([str isEqualToString:@"4"]){
                [nature addObject:@"外资"];

            }else if ([str isEqualToString:@"5"]){
                [nature addObject:@"有限责任公司"];

            }else if ([str isEqualToString:@"6"]){
                [nature addObject:@"股份有限公司"];

            }else if ([str isEqualToString:@"7"]){
                [nature addObject:@"上市公司"];

            }else if ([str isEqualToString:@"8"]){
                [nature addObject:@"非上市公司"];

            }else{
                [nature addObject:@"其他类型"];

            }

        }
        NSString *res2 = [nature componentsJoinedByString:@","];
        
        detailModel.NATURE = res2;
        
        [self.dataArray addObject:detailModel];
        
        [self setUp:model];
        
    } failure:^(NSError *error) {
        
    }];


}

- (void)setUp:(proviceModel *)model{

    DetailView *view1 = [DetailView withDetailView];
    
    view1.model = model;
    
    [self.view addSubview:view1];

//存放按钮的view
    UIView *view2 = [[UIView alloc] init];
    [self.view addSubview:view2];
    view2.backgroundColor = [UIColor grayColor];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(view1.mas_bottom);
        
        make.left.equalTo(self.view.mas_left);
        
        make.right.equalTo(self.view.mas_right);
        
        make.height.equalTo(@40);
        
    }];
    
//按钮
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [view2 addSubview:btn1];
    [btn1 setTitle:@"企业负责人信息" forState:UIControlStateNormal];
    [btn1 addTarget:self action:@selector(btn1Click) forControlEvents:UIControlEventTouchUpInside];
    [btn1.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(view2.mas_top);
        
        make.left.equalTo(view2.mas_left);
        
        make.bottom.equalTo(view2.mas_bottom);
        
        make.width.equalTo(@100);
    }];
    
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [view2 addSubview:btn2];
    [btn2.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [btn2 addTarget:self action:@selector(Btn2Click) forControlEvents:UIControlEventTouchUpInside];
    [btn2 setTitle:@"企业营业信息" forState:UIControlStateNormal];
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(view2.mas_top);
        
        make.left.equalTo(btn1.mas_right);
        
        make.bottom.equalTo(view2.mas_bottom);
        
        make.width.equalTo(@100);
    }];
    
//点击按钮需要切换的视图
    UIView *view3 = [[UIView alloc] init];
    
    self.view3 = view3;
    
    [self.view addSubview:view3];
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(view2.mas_bottom);
        
        make.left.equalTo(self.view.mas_left);
        
        make.right.equalTo(self.view.mas_right);
        
        make.bottom.equalTo(self.view.mas_bottom).mas_offset(-49);
    }];
    
//企业负责人信息
    FirstView *first = [FirstView WithFirstView];
    
    self.firstView = first;
    
    ProviceDetailModel *Detailmodel = self.dataArray.firstObject;
    
    first.model = Detailmodel;
    
    [self.view3 addSubview:first];
}
//企业负责人信息 按钮点击
- (void)btn1Click{
    [self.view3.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
   
    [self.view3 addSubview:self.firstView];
    
    
}
//企业营业信息 按钮点击
- (void)Btn2Click{

    [self.view3.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    SecondView *sec = [SecondView WithSecondView];
    
    ProviceDetailModel *Detailmodel = self.dataArray.firstObject;
    
    sec.model = Detailmodel;
    
    [self.view3 addSubview:sec];

    [sec mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view3.mas_top);
        make.left.equalTo(self.view3.mas_left);
        make.right.equalTo(self.view3.mas_right);
        make.bottom.equalTo(self.view3.mas_bottom);
    }];
    
}







@end
